﻿using System;
using UnityEngine;
namespace Z1029.NoteEditor
{
    public class Utils
    {
        public static float? GetValueFromTimeAndValue(double now, float startTime, float endTime, float startVal, float endVal)
        {
            if (now > startTime && now < endTime)
            {
                double num = (now - startTime) / (endTime - startTime);
                num = (endVal - startVal) * num + startVal;
                return (float)num;
            }
            return null;
        }

        public static double GetTimeFromBeatTime(float beatTime, float bpm)
        {
            double time = beatTime * (60 / bpm) / LevelController.density;
            return time;
        }

        public static int GetBeatTimeFromTime(double time, float bpm)
        {
            int btime = Mathf.RoundToInt((float)time * LevelController.density / (60 / bpm));
            return btime;
        }
    }

    public class TransformUtils
    {
        public static bool MouseInView(Vector2 pos, RectTransform rect)
        {
            throw new NotImplementedException();
            return (pos.x < rect.anchoredPosition.x);
        }

        public static Vector3 GetV3FromTimeAndValue(float now, float startTime, float endTime, Vector3 startVal, Vector3 endVal)
        {
            Vector3 result;
            result.x = (float)Utils.GetValueFromTimeAndValue(now, startTime, endTime, startVal.x, endVal.x);
            result.y = (float)Utils.GetValueFromTimeAndValue(now, startTime, endTime, startVal.y, endVal.y);
            result.z = (float)Utils.GetValueFromTimeAndValue(now, startTime, endTime, startVal.z, endVal.z);
            return result;
        }

        public static Vector3 GetPosFormEvent(TransformEvent te, float time)
        {
            return TransformUtils.GetV3FromTimeAndValue(time, te.startTime, te.endTime, te.startPos, te.endPos);
        }

        public static Vector3 GetRotV3FormEvent(TransformEvent te, float time)
        {
            return TransformUtils.GetV3FromTimeAndValue(time, te.startTime, te.endTime, te.startRot, te.endRot);
        }

        public static Quaternion GetRotFormEvent(TransformEvent te, float time)
        {
            return Quaternion.Euler(TransformUtils.GetRotV3FormEvent(te, time));
        }

        public static Vector3 GetPosFormEvents(TransformEvent[] te, float time)
        {
            for (int i = 0; i < te.Length; i++)
            {
                if (TransformUtils.GetPosFormEvent(te[i], time) != Vector3.zero)
                {
                    return TransformUtils.GetPosFormEvent(te[i], time);
                }
            }
            return Vector3.zero;
        }

        public static Vector3 GetRotV3FormEvents(TransformEvent[] te, float time)
        {
            for (int i = 0; i < te.Length; i++)
            {
                if (TransformUtils.GetRotV3FormEvent(te[i], time) != Vector3.zero)
                {
                    return TransformUtils.GetRotV3FormEvent(te[i], time);
                }
            }
            return Vector3.zero;
        }

        public static Quaternion GetRotFormEvents(TransformEvent[] te, float time)
        {
            return Quaternion.Euler(TransformUtils.GetRotV3FormEvents(te, time));
        }

        public static Vector3 GetLocalPosition(Vector3 worldPosition, Transform parent)
        {
            Vector3 localPosition = parent.InverseTransformPoint(worldPosition);
            return localPosition;
        }

        [Serializable]
        public struct TransformEvent
        {
            public float startTime, endTime;
            public Vector3 startPos, endPos, startRot, endRot;
        }
    }

}